package edu.unh.sustain;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.util.Log;

public class EnergyReading {
	private Document doc;
	private boolean day = false;
	private boolean week = false;
	private boolean month = false;
	private boolean year = false;
	private String span;
	private String pDate;
	private NodeList electric;
	private NodeList water;
	private NodeList gas;
	private double hVal;
	private int mIndex;
	private List<Double[]> values = new ArrayList<Double[]>();
	private List<double[]> dates = new ArrayList<double[]>();
	private List<Double> eDoubles;
	private List<Double> wDoubles;
	private List<Double> gDoubles;
	
	EnergyReading(Document d, String s, int m)
	{
		doc = d;
		span = s;
		mIndex = m;
		
		hVal = 0.0;
		pDate = doc.getElementsByTagName("prettydate").item(0).getTextContent();
		setPeriod();
		setUtilities();
		buildDates();
		buildValues();
	}
	
	private void setPeriod()
	{
		if(span.compareTo("Day") == 0)
		{
			day = true;
		} else if(span.compareTo("Week") == 0) {
			week = true;
		} else if(span.compareTo("Month") == 0) {
			month = true;
		} else if(span.compareTo("Year") == 0) {
			year = true;
		}
	}
	
	private void setUtilities()
	{
		water = doc.getElementsByTagName("Water");
		Log.w("water len", Integer.toString(water.getLength()));
		if(water.getLength() != 0)
		{
			wDoubles = new ArrayList<Double>();
		}
		
		electric = doc.getElementsByTagName("Electric");
		Log.w("elec len", Integer.toString(electric.getLength()));
		if(electric.getLength() != 0)
		{
			eDoubles = new ArrayList<Double>();
		}
			
		gas = doc.getElementsByTagName("Gas");
		Log.w("gas len", Integer.toString(gas.getLength()));
		if(gas.getLength() != 0)
		{
			gDoubles = new ArrayList<Double>();
		}
	}
	
	private void buildDates()
	{
		if(day)
		{
			dates.add(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
									21, 22, 23, 24});
		} else if(week) {
			dates.add(new double[] {1, 2, 3, 4, 5, 6, 7});
		} else if(month) {
			if(mIndex == 1 || mIndex == 3 || mIndex == 5 || mIndex == 7 || mIndex == 8 || mIndex == 10 ||
					mIndex == 12)
				dates.add(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
											19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31});
			else if(mIndex == 2)
				dates.add(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
						19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29});
			else
				dates.add(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
						19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30});
		} else if(year) {
			dates.add(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
		}
	}
	
	private void buildValues()
	{
		String temp;
		Double[] ed;
		Double[] wd;
		Double[] gd;
		
		// initialize arrays
		ed = new Double[dates.get(0).length];
		wd = new Double[dates.get(0).length];
		gd = new Double[dates.get(0).length];
		
		int e = 0;
		int w = 0;
		int g = 0;
		// populate arrays
		for(int i = 0; i < dates.get(0).length; i++)
		{
			if(electric.getLength() != 0)
			{				
				if(e < electric.getLength() - 4)
				{
					temp = electric.item(i).getTextContent();
					
					eDoubles.add(new Double(temp));
					if(Double.valueOf(temp) > hVal)
					{
						hVal = Double.valueOf(temp);
					}
				}
				else
					eDoubles.add(new Double(0.0));
				
				e++;
			}
			
			if(water.getLength() != 0) 
			{
				if(w < water.getLength() - 4)
				{
					temp = water.item(i).getTextContent();
					
					wDoubles.add(new Double(temp));
					if(Double.valueOf(temp) > hVal)
					{
						hVal = Double.valueOf(temp);
					}
				}
				else
					wDoubles.add(new Double(0.0));
				
				w++;
			}
			
			if(gas.getLength() != 0) 
			{
				if(g < gas.getLength() - 4)
				{
					temp = gas.item(i).getTextContent();
					
					gDoubles.add(new Double(temp));
					if(Double.valueOf(temp) > hVal)
					{
						hVal = Double.valueOf(temp);
					}
				}
				else
					gDoubles.add(new Double(0.0));
				
				g++;
			}
		}
		
		if(electric.getLength() != 0)
		{
			eDoubles.toArray(ed);
			values.add(ed);
		}
		
		if(water.getLength() != 0)
		{
			wDoubles.toArray(wd);
			values.add(wd);
		}
		
		if(gas.getLength() != 0)
		{
			gDoubles.toArray(gd);
			values.add(gd);
		}
	}
	
	public int getHighVal()
	{
		return (int)hVal + 50;
	}
	
	public List<Double[]> getValues()
	{
		return values;
	}
	
	public List<double[]> getDates()
	{
		return dates;
	}
	
	public String getPDate()
	{
		return pDate;
	}
	
	public boolean hasElectric()
	{
		if(electric.getLength() != 0)
			return true;
		else
			return false;
	}
	
	public boolean hasGas()
	{
		if(gas.getLength() != 0)
			return true;
		else
			return false;
	}
	
	public boolean hasWater()
	{
		if(water.getLength() != 0)
			return true;
		else
			return false;
	}
	
	public boolean isDay()
	{
		return day;
	}
	
	public boolean isWeek()
	{
		return week;
	}
	
	public boolean isMonth()
	{
		return month;
	}
	
	public boolean isYear()
	{
		return year;
	}
}
